var listColumns = [[
    {field: "id", checkbox: true},
    {field: "code", title: "CODE", align: "center", width: 140},
    {field: "dictName", title: "中文名称", align: "center", width: 100},
    {field: "dictValue", title: "值", align: "center", width: 120},
    {field: "updator", title: "更新人", align: "center", width: 120},
    {field: "updateTime", title: "更新时间", align: "center", width: 140},
    {
        field: "handle", title: "操作", align: "center", width: 120, formatter: function (value, rowData,index) {
            return '<a class="layui-btn layui-btn-xs"  onclick="editData('+index+')">编辑</a>'
        }
    }

]];

layui.config({
    base: "../static/agent-ui/js/"
});

$.fn.toJSON = function () {
    var o = {};
    var a = this.serializeArray();
    $.each(a, function () {
        var name = this.name;
        var value = this.value;
        var paths = this.name.split(".");
        var len = paths.length;
        var obj = o;
        $.each(paths, function (i, e) {
            if (i == len - 1) {
                if (obj[e]) {
                    if (!obj[e].push) {
                        obj[e] = [obj[e]];
                    }
                    obj[e].push(value || '');
                } else {
                    obj[e] = value || '';
                }
            } else {
                if (!obj[e]) {
                    obj[e] = {};
                }
            }
            obj = o[e];
        });
    });
    return o;
};

layui.use(["laypage", "layer", "validator", "tools", "form", "select", "laydate"], function () {
    var form = layui.form;
    var date = layui.laydate;
    date.render({
        elem: '#ringTime',
        type: 'datetime',
        range: true
    });


    var layer=layui.layer;

    $(function () {
        $('#list').datagrid({
            columns: listColumns,
            fitColumns: false,
            fit: true,
            singleSelect: true,
            pagination: true,
            pageSize: 30,
            pageList: [30, 50, 100, 1000],
            toolbar: '#receivedListHeader',
            method: 'get',
            url: ctx +'/dict/list',
            onLoadSuccess: function () {


            }
        });
    });

    $("#addData").on("click", function(){
        /* 再弹出添加界面 */
        layer.open({
            type:1,
            title:"添加字典",
            skin:"myclass",
            area:["50%"],
            content:$("#add").html(),
            success:function (layero,index) {
                layero.addClass('layui-form');
                layero.find('.layui-layer-btn0').attr('lay-filter', 'fromContent').attr('lay-submit', '');//将按钮弄成能提交的
            },
            btn:['提交','关闭'],
            yes:function (index,layero) {
                form.on('submit(fromContent)', function (data) {
                    add(index,layero)
                });
            },
            btn2:function (index,layero) {
                layer.close(index);
            }
        });
    });

    function add(index,layero) {
        $.ajax({
            url:ctx+'/dict/insert',
            type : 'POST',
            data:$(layero).find("form[id=\"add_form\"]").toJSON(),
            success:function (result) {
                var layer = layui.layer;
                var json = JSON.parse(result);
                if(json.code=='0'){
                    layer.close(index);
                    $('#list').datagrid('reload');
                }else{
                    layer.open({
                        title: '报错了'
                        ,content: json.msg
                    });
                }
            },
            error:function (result) {
                var json = JSON.parse(result);
                var layer = layui.layer;
                layer.open({
                    title: '报错了'
                    ,content: '更新失败'
                });

            }
        });
    }

});

// 编辑
function  editData(index) {
    var rows = $('#list').datagrid('getRows');
    var json = rows[index];
    var form = layui.form;
    var edit_form = "edit";
    if(json.code == 'system-model'){
        edit_form = "edit_systemModel";
    }
    layer.open({
        type:1,
        title:"编辑字典",
        skin:"myclass",
        area:["50%","85%"],
        content:$("#"+edit_form).html(),
        success:function (layero,index) {
            layero.addClass('layui-form');
            layero.find('.layui-layer-btn0').attr('lay-filter', 'fromContent').attr('lay-submit', '');//将按钮弄成能提交的
            $(layero).find('input[name=code]').val(json.code);
            $(layero).find('input[name=dictName]').val(json.dictName);
            if(json.code == 'system-model'){
                $(layero).find('select[name=dictValue]').val(json.dictValue=='抢单模式'?'0':'1');
            }else{
                $(layero).find('textarea[name=dictValue]').val(json.dictValue);
            }
            form.render('select'); //刷新select选择框渲染
        },
        btn:['提交','关闭'],
        yes:function (index,layero) {
            form.on('submit(fromContent)', function (data) {
                update(index,json.id,layero,edit_form)
            });
        },
        btn2:function (index,layero) {
            layer.close(index);
        }
    });
}

function update(index,id,layero,edit_form) {
    $.ajax({
        url:ctx+'/dict/update/'+id,
        type : 'POST',
        data:$(layero).find("form[id="+edit_form+"_form]").toJSON(),
        success:function (result) {
            var layer = layui.layer;
            var json = JSON.parse(result);
            if(json.code=='0'){
                layer.close(index);
                $('#list').datagrid('reload');
            }else{
                layer.open({
                    title: '报错了'
                    ,content: json.msg
                });
            }
        },
        error:function (result) {
            var json = JSON.parse(result);
            var layer = layui.layer;
            layer.open({
                title: '报错了'
                ,content: '更新失败'
            });

        }
    });
}




